package userinterface;

import interfacemanagement.ConfigWindowManager;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.io.File;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class ConfigurationWindow extends PopupWindow {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel pnlPrincipal = null;
	private JTabbedPane pnlTabs = null;
	private JButton btnSave = null;
	private JButton btnCancel = null;
	private JPanel btnPanel = null;
	private JPanel pnlGeneral = null;
	private JPanel pnlAdvanced = null;
	private JPanel pnlTitle = null;

	private ConfigWindowManager manager;
	private JLabel lblInitialPath = null;
	private JTextField tfInitialDirectory = null;
	private JButton btnBrowse = null;
	private JLabel lblFirstTime = null;
	private JCheckBox chkFirstTime = null;
	private JLabel lblPaused = null;
	private JCheckBox chkPaused = null;
	private JLabel lblSearchCacheTime = null;
	private JLabel lblVocabularyCacheTime = null;
	private JTextField tfSearchCacheTime = null;
	private JTextField tfVocabularyCacheTime = null;
	private JLabel lblOverwriteValues = null;
	private JCheckBox chkOverwrite = null;
	private JFileChooser fcInitialDirectory = null;

	/**
	 * This is the default constructor
	 */
	public ConfigurationWindow() {
		super();
		this.basePath = this.getClass().getResource("/").toString();
		this.manager = new ConfigWindowManager();
		initialize();
		this.setConfigurationValues();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(488, 308);
		this.setLocation(new Point(300, 300));
		this.setName("frmConfig");
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("HawarInDXR");
		URL url = getBaseURL("/images/config.png");
		if (url != null)
			this.setIconImage(new ImageIcon(url).getImage());
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowOpened(java.awt.event.WindowEvent e) {
				// ConfigurationWindow.this.paintAll(ConfigurationWindow.this.getGraphics());
				setConfigurationValues();
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(1);
			jContentPane = new JPanel();
			jContentPane.setLayout(gridLayout);
			jContentPane.add(getPnlPrincipal(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes pnlPrincipal
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPnlPrincipal() {
		if (pnlPrincipal == null) {
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 0;
			gridBagConstraints21.insets = new Insets(5, 50, 5, 5);
			gridBagConstraints21.gridy = 0;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridy = 0;
			gridBagConstraints11.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints11.gridwidth = 3;
			gridBagConstraints11.gridx = 0;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridwidth = 3;
			gridBagConstraints3.anchor = GridBagConstraints.EAST;
			gridBagConstraints3.insets = new Insets(0, 5, 0, 20);
			gridBagConstraints3.gridy = 2;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = 1;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridwidth = 3;
			gridBagConstraints.insets = new Insets(0, 0, 0, 0);
			gridBagConstraints.gridx = 0;
			pnlPrincipal = new JPanel();
			pnlPrincipal.setLayout(new GridBagLayout());
			pnlPrincipal.add(getPnlTabs(), gridBagConstraints);
			pnlPrincipal.add(getBtnPanel(), gridBagConstraints3);
			pnlPrincipal.add(getPnlTitle(), gridBagConstraints11);
		}
		return pnlPrincipal;
	}

	/**
	 * This method initializes pnlTabs
	 * 
	 * @return javax.swing.JTabbedPane
	 */
	private JTabbedPane getPnlTabs() {
		if (pnlTabs == null) {
			pnlTabs = new JTabbedPane();
			pnlTabs.setTabPlacement(JTabbedPane.BOTTOM);
			pnlTabs.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			pnlTabs.setPreferredSize(new Dimension(152, 100));
			pnlTabs.addTab("General", null, getPnlGeneral(),
					"General settings for the application");
			pnlTabs
					.addTab("Advanced", null, getPnlAdvanced(),
							"Advanced settings, which have implications during the indexation");
		}
		return pnlTabs;
	}

	/**
	 * This method initializes btnSave
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnSave() {
		if (btnSave == null) {
			btnSave = new JButton();
			btnSave.setText("");
			btnSave.setToolTipText("Save");
			btnSave.setPreferredSize(new Dimension(60, 35));
			URL url = getBaseURL("/images/accept.png");
			if (url != null)
				btnSave.setIcon(new ImageIcon(url, ""));

			btnSave.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (validateConfigValues()) {
						writeConfigurationValues();
						ConfigurationWindow.this.dispose();
					} else {
						JOptionPane.showMessageDialog(ConfigurationWindow.this,
								"At least one value is not valid",
								"Error Message", JOptionPane.ERROR_MESSAGE);
					}
				}
			});
		}
		return btnSave;
	}

	/**
	 * This method initializes btnCancel
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setText("");
			btnCancel.setToolTipText("Cancel");
			btnCancel.setPreferredSize(new Dimension(60, 35));
			URL url = getBaseURL("/images/cancel.png");
			if (url != null)
				btnCancel.setIcon(new ImageIcon(url, ""));
			btnCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ConfigurationWindow.this.dispose();
				}
			});
		}
		return btnCancel;
	}

	/**
	 * This method initializes btnPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getBtnPanel() {
		if (btnPanel == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints2.gridy = -1;
			gridBagConstraints2.gridx = -1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints1.gridy = -1;
			gridBagConstraints1.gridx = -1;
			btnPanel = new JPanel();
			btnPanel.setLayout(new GridBagLayout());
			btnPanel.add(getBtnSave(), gridBagConstraints1);
			btnPanel.add(getBtnCancel(), gridBagConstraints2);
		}
		return btnPanel;
	}

	/**
	 * This method initializes pnlGeneral
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPnlGeneral() {
		if (pnlGeneral == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 1;
			gridBagConstraints4.gridy = 0;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 1;
			gridBagConstraints12.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints12.anchor = GridBagConstraints.WEST;
			gridBagConstraints12.gridy = 3;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints10.anchor = GridBagConstraints.EAST;
			gridBagConstraints10.gridy = 3;
			lblPaused = new JLabel();
			lblPaused.setText("Paused");
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 1;
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints9.gridy = 2;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints8.anchor = GridBagConstraints.EAST;
			gridBagConstraints8.gridy = 2;
			lblFirstTime = new JLabel();
			lblFirstTime.setText("First Execution");
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 2;
			gridBagConstraints7.insets = new Insets(5, 0, 5, 5);
			gridBagConstraints7.gridy = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.gridy = 1;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.gridx = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints5.anchor = GridBagConstraints.EAST;
			gridBagConstraints5.gridy = 1;
			lblInitialPath = new JLabel();
			lblInitialPath.setText("Initial Directory");
			pnlGeneral = new JPanel();
			pnlGeneral.setLayout(new GridBagLayout());
			pnlGeneral.setPreferredSize(new Dimension(0, 0));
			pnlGeneral.add(lblInitialPath, gridBagConstraints5);
			pnlGeneral.add(getTfInitialDirectory(), gridBagConstraints6);
			pnlGeneral.add(getBtnBrowse(), gridBagConstraints7);
			pnlGeneral.add(lblFirstTime, gridBagConstraints8);
			pnlGeneral.add(getChkFirstTime(), gridBagConstraints9);
			pnlGeneral.add(lblPaused, gridBagConstraints10);
			pnlGeneral.add(getChkPaused(), gridBagConstraints12);
			pnlGeneral.add(getFcInitialDirectory(), gridBagConstraints4);
		}
		return pnlGeneral;
	}

	/**
	 * This method initializes pnlAdvanced
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPnlAdvanced() {
		if (pnlAdvanced == null) {
			GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
			gridBagConstraints18.gridx = 1;
			gridBagConstraints18.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints18.anchor = GridBagConstraints.WEST;
			gridBagConstraints18.gridy = 2;
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.gridx = 0;
			gridBagConstraints17.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints17.anchor = GridBagConstraints.EAST;
			gridBagConstraints17.gridy = 2;
			lblOverwriteValues = new JLabel();
			lblOverwriteValues.setText("Overwrite Documents");
			lblOverwriteValues.setHorizontalAlignment(SwingConstants.RIGHT);
			lblOverwriteValues.setPreferredSize(new Dimension(130, 20));
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints16.gridy = 1;
			gridBagConstraints16.weightx = 1.0;
			gridBagConstraints16.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints16.anchor = GridBagConstraints.WEST;
			gridBagConstraints16.gridx = 1;
			GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
			gridBagConstraints15.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints15.gridy = 0;
			gridBagConstraints15.weightx = 1.0;
			gridBagConstraints15.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints15.anchor = GridBagConstraints.WEST;
			gridBagConstraints15.gridx = 1;
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.gridx = 0;
			gridBagConstraints14.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints14.anchor = GridBagConstraints.EAST;
			gridBagConstraints14.gridy = 1;
			lblVocabularyCacheTime = new JLabel();
			lblVocabularyCacheTime.setText("Vocabulary Cach� Time (min.)");
			lblVocabularyCacheTime.setHorizontalAlignment(SwingConstants.RIGHT);
			lblVocabularyCacheTime.setPreferredSize(new Dimension(170, 20));
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.gridx = 0;
			gridBagConstraints13.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints13.anchor = GridBagConstraints.EAST;
			gridBagConstraints13.gridy = 0;
			lblSearchCacheTime = new JLabel();
			lblSearchCacheTime.setText("Search Cach� Time (min.)");
			lblSearchCacheTime.setHorizontalAlignment(SwingConstants.RIGHT);
			lblSearchCacheTime.setPreferredSize(new Dimension(150, 20));
			pnlAdvanced = new JPanel();
			pnlAdvanced.setLayout(new GridBagLayout());
			pnlAdvanced.add(lblSearchCacheTime, gridBagConstraints13);
			pnlAdvanced.add(lblVocabularyCacheTime, gridBagConstraints14);
			pnlAdvanced.add(getTfSearchCacheTime(), gridBagConstraints15);
			pnlAdvanced.add(getTfVocabularyCacheTime(), gridBagConstraints16);
			pnlAdvanced.add(lblOverwriteValues, gridBagConstraints17);
			pnlAdvanced.add(getChkOverwrite(), gridBagConstraints18);
		}
		return pnlAdvanced;
	}

	/**
	 * This method initializes pnlTitle
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPnlTitle() {
		if (pnlTitle == null) {
			pnlTitle = new JPanel();
			pnlTitle.setLayout(new GridBagLayout());
		}
		return pnlTitle;
	}

	/**
	 * Sets the actual values, reading them from a .properties file
	 */
	private void setConfigurationValues() {
		this.getTfInitialDirectory().setText(manager.getDefaultDir());
		this.getChkFirstTime().setSelected(manager.isFirstTime());
		this.getChkPaused().setSelected(manager.isPaused());

		this.getTfSearchCacheTime().setText(manager.getSearchCacheTime());
		this.getTfVocabularyCacheTime().setText(
				manager.getVocabularyCacheTime());
		this.getChkOverwrite().setSelected(manager.isOverwriteEnabled());
	}

	private boolean validateConfigValues() {
		try {
			Integer.parseInt(getTfSearchCacheTime().getText());
			Integer.parseInt(getTfVocabularyCacheTime().getText());
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	private void writeConfigurationValues() {
		String default_dir = getTfInitialDirectory().getText();
		String search_cache_time = getTfSearchCacheTime().getText();
		String vocabulary_cache_time = getTfVocabularyCacheTime().getText();
		manager.writeConfig(default_dir, search_cache_time,
				vocabulary_cache_time);
	}

	private void openFileChooser() {
		File default_dir = new File(manager.getDefaultDir());
		fcInitialDirectory.setCurrentDirectory(default_dir);
		fcInitialDirectory.setVisible(true);
		int returnval = fcInitialDirectory
				.showOpenDialog(ConfigurationWindow.this);
		if (returnval == JFileChooser.APPROVE_OPTION) {
			tfInitialDirectory.setText(fcInitialDirectory.getSelectedFile()
					.getAbsolutePath());
		}
	}

	/**
	 * This method initializes tfInitialDirectory
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTfInitialDirectory() {
		if (tfInitialDirectory == null) {
			tfInitialDirectory = new JTextField();
			tfInitialDirectory.setPreferredSize(new Dimension(300, 23));
		}
		return tfInitialDirectory;
	}

	/**
	 * This method initializes btnBrowse
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnBrowse() {
		if (btnBrowse == null) {
			btnBrowse = new JButton();
			btnBrowse.setText("...");
			btnBrowse.setPreferredSize(new Dimension(35, 20));
			btnBrowse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					openFileChooser();
				}
			});
		}
		return btnBrowse;
	}

	/**
	 * This method initializes chkFirstTime
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getChkFirstTime() {
		if (chkFirstTime == null) {
			chkFirstTime = new JCheckBox();
			chkFirstTime.setEnabled(false);
		}
		return chkFirstTime;
	}

	/**
	 * This method initializes chkPaused
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getChkPaused() {
		if (chkPaused == null) {
			chkPaused = new JCheckBox();
			chkPaused.setEnabled(false);
		}
		return chkPaused;
	}

	/**
	 * This method initializes tfSearchCacheTime
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTfSearchCacheTime() {
		if (tfSearchCacheTime == null) {
			tfSearchCacheTime = new JTextField();
			tfSearchCacheTime.setPreferredSize(new Dimension(50, 22));
			tfSearchCacheTime.setEditable(true);
			tfSearchCacheTime.setEnabled(true);
		}
		return tfSearchCacheTime;
	}

	/**
	 * This method initializes tfVocabularyCacheTime
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTfVocabularyCacheTime() {
		if (tfVocabularyCacheTime == null) {
			tfVocabularyCacheTime = new JTextField();
			tfVocabularyCacheTime.setPreferredSize(new Dimension(50, 22));
			tfVocabularyCacheTime.setEnabled(true);
			tfVocabularyCacheTime.setEditable(true);
		}
		return tfVocabularyCacheTime;
	}

	/**
	 * This method initializes chkOverwrite
	 * 
	 * @return javax.swing.JCheckBox
	 */
	private JCheckBox getChkOverwrite() {
		if (chkOverwrite == null) {
			chkOverwrite = new JCheckBox();
			chkOverwrite
					.setToolTipText("If enabled, the data of existent documents will be overwritten");
			chkOverwrite.setEnabled(false);
		}
		return chkOverwrite;
	}

	/**
	 * This method initializes fcInitialDirectory
	 * 
	 * @return javax.swing.JFileChooser
	 */
	private JFileChooser getFcInitialDirectory() {
		if (fcInitialDirectory == null) {
			fcInitialDirectory = new JFileChooser();
			fcInitialDirectory
					.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			fcInitialDirectory.setDialogTitle("Select Directory");
			fcInitialDirectory.setControlButtonsAreShown(true);
			fcInitialDirectory.setVisible(false);
		}
		return fcInitialDirectory;
	}

} // @jve:decl-index=0:visual-constraint="184,9"
